package org.jxnd.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.User;
import org.jxnd.bean.UserInfo;
import org.jxnd.dao.IUserDao;
import org.jxnd.tools.DBTools;

public class UserDaoImpl implements IUserDao {
	@Override
	public boolean getUserName(String name) {
		String sql="select * from userinfo where name=?";
		List<Object> list=new ArrayList<Object>();
		list.add(name);
		ResultSet rs=DBTools.getReusultSet(sql, list);
		try {
			if (rs.next())
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//try {
				//rs.close();
				DBTools.close();
			//} catch (SQLException e) {
				//e.printStackTrace();
			//}
		}
		return false;
	}

	@Override
	public UserInfo getUserByNameAndPwd(String name, String pwd) {
		String sql="select userinfo.id as userinfo_id,userinfo.name as userinfo_name,userinfo.pwd as userinfo_pwd,userinfo.rolesid as userinfo_rolesid,roles.id as roles_id,roles.name as roles_name from userinfo,roles where userinfo.name=? and userinfo.pwd=? and userinfo.rolesid=roles.id";
		List<Object> plist=new ArrayList<Object>();
		plist.add(name);
		plist.add(pwd);
		return DBTools.getObject(UserInfo.class, sql, plist);
	}

	@Override
	public List<UserInfo> getUserPage(int pageIndex, int pageSize) {
		String sql="select * from (select rownum as r_id,userinfo.id as userinfo_id,userinfo.name as userinfo_name,userinfo.pwd as userinfo_pwd,userinfo.rolesid as userinfo_rolesid,roles.id as roles_id,roles.name as roles_name from userinfo,roles where userinfo.rolesid=roles.id)where r_id>? and r_id<?";
		List<Object> plist=new ArrayList<Object>();
		plist.add((pageIndex-1)*pageSize);
		plist.add(pageIndex*pageSize+1);
		return DBTools.getListBySql(UserInfo.class, sql, plist);
	}

	@Override
	public int deleteUsers(int id) {
		return DBTools.deleteObject(UserInfo.class, id);
	}
}


















